<?php
/**
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-4-24
 * Time: 下午7:22
 * 范例套票的tid 16865
 */


header("Content-type: text/html; charset=utf-8");
error_reporting(E_ERROR | E_PARSE | E_WARNING);
include("/var/www/html/new/d/common/header.html");
include_once '/var/www/html/new/com.inc.php';
$sql = "select group_id from pft_member where id={$_SESSION['sid']} limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$group_id = $GLOBALS['le']->f('group_id');
if($group_id!=4)
    exit('Access Denied');
if(!$_GET['begintime'] || !$_GET['endtime'])
    goto mark_one;
$begintime = $_GET['begintime'];
$endtime   = $_GET['endtime'];

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$soap = new SoapClient(null,array(
    "location" =>"http://localhost/open/openService/pft_insideMX.php",
    "uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true")
);
if($_GET['status']){
    foreach ($_GET['status'] as $k => $v) {
        $st = explode('|',$v);
        foreach ($st as $ke => $va) {
            $status[$va] = $va;
        }
    }
    foreach ($status as $k => $v) {
        $sta .= $v.'|';
    }
    $status = rtrim($sta,'|');
}else
    $_GET['status']='';
switch ($_GET['aom']) {
	case 'aid':
        $x = simplexml_load_string(
            $soap->Order_Globle_Search(
                "",'', "", "", "", "", "$begintime", "$endtime", "", "",
                "", "", "", "", "", "$status", "", "", "", "",
                0, 9999, 0, 0, "", 0, "", 0, "", "",
                $_SESSION['sid'], 0, 0, 0, ""),
            'SimpleXMLElement',LIBXML_COMPACT
        );
		break;
	
	default:

        $x = simplexml_load_string(
            $soap->Order_Globle_Search(
                "",$_SESSION['sid'], "", "", "", "", "$begintime", "$endtime", "", "",
                "", "", "", "", "", "$status", "", "", "", "",
                0, 9999, 0, 0, "", 0, "", 0, "", "",
                '', 0, 0, 0, ""),
            'SimpleXMLElement',LIBXML_COMPACT
        );
       /*
        $x = simplexml_load_string(
            $soap->Order_Globle_Search(
                "","", "", "", "", "", "", "", "", "",
                "", "", "1040300", "", "", "", "", "", "", "",
                0, 9999, 0, 0, "", 0, "", 0, "", "",
                '', 0, 0, 0, ""),
            'SimpleXMLElement',LIBXML_COMPACT
        );
        */
		break;
}
/**
 * 景区要可以看到明细的报表 包含套票的各种子票的
 * 查到的订单 不是套票的都continue
 * 最后再去结算查到的所以套票的子票数量
 * 观光车和索道的价格都是按供货价来算
 * 酒店的价格 就按套票减去观光车和索道和景区门票的价格
 */
//if($_GET['cs']){
//    echo "<pre>";
//    print_r($_GET['status']);
//    exit;
//}
$ticket_data = include_once '/var/www/html/new/d/module/zax/yd_report/ticket_data.php';
$tmp = $aid =array();
//print_r($GLOBALS['aid']);
//exit;
foreach ($x->Rec as $k => $v) {
    if((string)$v->UUp_type!='F')
        continue;
    $mid = mid($v);
    /**
     * 支付方式的区别
     */
    switch ((int)$v->UUpmode) {
    	case 4://现场支付 用零食价
            $tmp[(int)$v->UUtid][$mid]['local']['title'] = (string)$v->UUltitle;
            $tmp[(int)$v->UUtid][$mid]['local']['dname'] = $mid;
            $tmp[(int)$v->UUtid][$mid]['local']['tnum'] += 1;
            $tmp[(int)$v->UUtid][$mid]['local']['ti_num'] += (int)$v->UUtnum;
            $tmp[(int)$v->UUtid][$mid]['local']['tmoney'] += (int)$v->UUtnum * $ticket_data[(int)$v->UUtid]['data_2']['lsj'];
    		break;
        case 2://授信支付
            $uutprice = second_hand($v);
            $tmp[(int)$v->UUtid][$mid]['credit']['title'] = (string)$v->UUltitle;
            $tmp[(int)$v->UUtid][$mid]['credit']['dname'] = $mid;
            $tmp[(int)$v->UUtid][$mid]['credit']['tnum'] += 1;
            $tmp[(int)$v->UUtid][$mid]['credit']['ti_num'] += (int)$v->UUtnum;
            $tmp[(int)$v->UUtid][$mid]['credit']['tmoney'] += (int)$v->UUtnum * $uutprice;
            break;
        case 1://支付宝在线支付
            $uutprice = second_hand($v);
            $tmp[(int)$v->UUtid][$mid]['online']['title'] = (string)$v->UUltitle;
            $tmp[(int)$v->UUtid][$mid]['online']['dname'] = $mid;
            $tmp[(int)$v->UUtid][$mid]['online']['tnum'] += 1;
            $tmp[(int)$v->UUtid][$mid]['online']['ti_num'] += (int)$v->UUtnum;
            $tmp[(int)$v->UUtid][$mid]['online']['tmoney'] += (int)$v->UUtnum * $uutprice;
    		break;
    }

}

foreach ($tmp as $k => $v){
    foreach ($v as $ke => $va){
        foreach ($va as $key => $val) {
            //j就是景(jing)点 c就是观光车(che) s就是索(suo)道 h就是hotel
            if($_SESSION['sid']==7132){//是云顶景区时 多输出一行子票内的明细需要用到子票的名称
                $tmp[$k][$ke][$key]['jname'] = $ticket_data[$k]['data'][0]['title'];
                $tmp[$k][$ke][$key]['cname'] = $ticket_data[$k]['data'][1]['title'];
                $tmp[$k][$ke][$key]['sname'] = $ticket_data[$k]['data'][2]['title'];
                if($ticket_data[$k]['data'][3])//套票含酒店类型时
                    $tmp[$k][$ke][$key]['hname'] = $ticket_data[$k]['data'][3]['title'];
            }
            switch ($key){//在线支付取零售价 其他类型取分销价
                case 'local':
                    $data = 'data_2';
                    break;
                default:
                    $data = 'data';
                    break;
            }
            $tmp[$k][$ke][$key]['jti_num'] =
                $val['ti_num'] * $ticket_data[$k][$data][0]['num'];
            $tmp[$k][$ke][$key]['cti_num'] =
                $val['ti_num'] * $ticket_data[$k][$data][1]['num'];
            $tmp[$k][$ke][$key]['cmoney'] =
                $tmp[$k][$ke][$key]['cti_num'] * $ticket_data[$k][$data][1]['price'];
            $tmp[$k][$ke][$key]['sti_num'] =
                $val['ti_num'] * $ticket_data[$k][$data][2]['num'];
            $tmp[$k][$ke][$key]['smoney'] =
                $tmp[$k][$ke][$key]['sti_num'] * $ticket_data[$k][$data][2]['price'];
            if($ticket_data[$k][$data][3]){
                $tmp[$k][$ke][$key]['jmoney'] =
                    $tmp[$k][$ke][$key]['jti_num'] * $ticket_data[$k][$data][0]['price'];
                $tmp[$k][$ke][$key]['hti_num'] =
                    $val['ti_num'] * $ticket_data[$k][$data][3]['num'];
                $tmp[$k][$ke][$key]['hmoney'] =
                    $val['tmoney'] - $tmp[$k][$ke][$key]['jmoney'] -
                    $tmp[$k][$ke][$key]['cmoney'] - $tmp[$k][$ke][$key]['smoney'];
            }else{
                $tmp[$k][$ke][$key]['jmoney'] =$val['tmoney'] -
                    $tmp[$k][$ke][$key]['cmoney'] - $tmp[$k][$ke][$key]['smoney'];
            }
        }
    }
}

//处理转分销价格 把整个订单传进来 返回值是int 价格
function second_hand($order){
    $prices = (string)$order->UUaids_price;
    if($prices==0)
        return (int)$order->UUtprice;
    $prices = explode(',',$prices);
    $aid = (string)$order->UUaids;
    $aid = explode(',',$aid);
    $s = array_search($_SESSION['sid'],$aid);
    switch ($_GET['aom']) {
        case 'aid'://最后一级供应商看到的价格直接就是订单的价格
            return $prices[$s] ? $prices[$s] : (int)$order->UUtprice;
            break;

        default://最后一级分销商商看到的价格直接就是订单的价格
            return $_SESSION['sid'] != (int)$order->UUmid ?
                        $prices[$s-1] : (int)$order->UUtprice;
            break;
    }

}
function mid($order){
    $aids = (string)$order->UUaids;
    $aid = explode(',',$aids);
    if(!$aid[2]){
        $GLOBALS['aid'][(int)$order->UUmid]=(int)$order->UUmid;
        return (int)$order->UUmid;
    }
    $GLOBALS['aid'][$aid[2]]=$aid[2];
    return $aid[2];
}

foreach ($GLOBALS['aid'] as $k => $v) {
    $mids .= $v.',';
}
$mids = trim($mids,',');
//print_r($aid);
//echo $mids;
//exit;
if($mids){
    $sql = "select id,dname from pft_member where id in ($mids)";
    $GLOBALS['le']->query($sql);
    $row = array();
    while($row=$GLOBALS['le']->fetch_assoc()){
        $dname[$row['id']] = $row['dname'];
    }
}
mark_one:
function pmode($type){
    switch ($type) {
        case 'local':
            echo '现场支付';
            break;
        case 'credit':
            echo '<span style="color:green">授信支付</span>';
            break;
        case 'online':
            echo '<span style="color:red">在线支付</span>';
            break;
    }
}
?>
<link rel="stylesheet" type="text/css" href="//www.12301.cc/shops/css/gmem.css"/>

<script language="javascript" src="//www.12301.cc/js/WdatePicker.js"></script>
<script src="//www.12301.cc/js/jsorder.js"></script>
<script type="text/javascript">
    var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
            , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
            , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
            , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
        return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
            window.location.href = uri + base64(format(template, ctx))
        }
    })()
    $("#txtStartTime").bind("click focus", function () {

        var endtimeTf = $dp.$('txtEndTime');
        WdatePicker({
            maxDate: '#F{$dp.$D(\'txtEndTime\')}',
            dateFmt: "yyyy-MM-dd",
            onpicked: function () { endtimeTf.focus(); }
        });
    });
    $("#txtEndTime").bind("click focus", function () {
        WdatePicker({
            minDate: '#F{$dp.$D(\'txtStartTime\')}',
            dateFmt: "yyyy-MM-dd"
        });
    });
</script>
<link rel="stylesheet" type="text/css" href="//www.12301.cc/css/member.css"/>
<style>
    .report{overflow: hidden;margin:10px 0; padding:15px; border:1px solid #eee; }
    .report span.part{ margin-right:20px;}
    input.time{    background: url("../images/rili.png") no-repeat scroll right center #FFFFFF;
        border: 1px solid #7F9DB9;
        height: 20px;
        line-height: 20px;
        width: 120px;}
    .total{ width:100%; text-align:center;}
    input.checkbox{ vertical-align:-2px; margin-right:2px;}
    .btn{ background:#eee; padding:4px 15px; *padding:4px 8px; background:url("../images/mc_title.png") no-repeat right top; color:#fff; border:none; cursor:pointer}
    #excel{margin-left:10px;}
    .red {color:red}
    .rank {width:1000px;margin:0 auto 10px;}
</style>
<div class="mem_right">
    <form action="" method="get"  name="form" >
        <div class="mem_time">
             <span class="lt">下单时间：</span>
             <span class="part">
                    <input type="text" name="begintime" id="begintime" class="time" value="<?=$begintime?>" onFocus="WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"/>
                    <label>至</label>
                    <input type="text" name="endtime" id="endtime" class="time" value="<?=$endtime?>" onFocus="WdatePicker({startDate:'%y-%M-01 23:59:59',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"/>
            </span>
            <span  class="part">
		<input class="checkbox" type="checkbox" name="status[]" <?=in_array("0|1|2|3|5|6|7",$_GET['status'])?"checked ":""?> value="0|1|2|3|5|6|7"/><label>全部</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=in_array("0|6",$_GET['status'])?"checked":""?> value="0|6"/><label>未使用</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=(in_array("1|7",$_GET['status']) )?"checked":""?> value="1|7"/><label>已使用</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=(in_array("2",$_GET['status']))?"checked":""?> value="2"/><label>已过期</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=(in_array("3|5",$_GET['status']))?"checked":""?> value="3|5"/><label>已取消</label>
	</span>
            <input type="submit" value="提交">
            <input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="导出到Excel">
            查询类型:<select name="aom" >value="提交"
                <option value="mid" >已买到报表</option>
                <option value="aid" <?=$_GET['aom']=='aid'?' selected="selected"':''?>>已卖出报表</option>
                </select>
        </div>
    </form>
</div>
</div>
<div class="rank">
    <div class="rankcon">
        <table class="ranktab" id='testTable'>
            <thead id="rankhd">
                <th>序号</th>
                <th class="tL">景区名称</th>
                <th class="tL">销售渠道</th>
                <th><span class="sort" onclick="sortTable('offmain',3, true ,this);">订单总数</span></th>
                <th><span class="sort" onclick="sortTable('offmain',4, true ,this);">门票总数</span></th>
                <th><span class="sort" onclick="sortTable('offmain',5, true ,this);">总金额</span></th>
                <th><span class="sort" onclick="sortTable('offmain',6, true ,this);">景点门票金额</span></th>
                <th><span class="sort" onclick="sortTable('offmain',7, true ,this);">观光车金额</span></th>
                <th><span class="sort" onclick="sortTable('offmain',8, true ,this);">索道金额</span></th>
                <th><span class="sort" onclick="sortTable('offmain',9, true ,this);">酒店金额</span></th>
                <th class="tL">备注</th>
                <th>支付方式</th>
            </thead>
        <tbody id="offmain">
        <?php
        if(count($tmp)):
        $m = 0;
        foreach ($tmp as $k => $v) {
            foreach ($v as $ke => $va){
                foreach ($va as $key => $val) {
        ?>
        <tr data-id="">
            <td><?=++$m?></td>
            <td><?=$val['title']?></td>
            <td><?=$dname[$val['dname']]?></td>
            <td><?=$val['tnum']?></td>
            <td><?=$val['ti_num']?></td>
            <td><?=$val['tmoney']/100?></td>
            <td><?=$val['jmoney']/100?></td>
            <td><?=$val['cmoney']/100?></td>
            <td><?=$val['smoney']/100?></td>
            <td><?=$val['hmoney']/100?></td>
            <td class="tL"></td>
            <td><?=pmode($key)?></td>
        </tr>
        <?if($_SESSION['sid']==7132){
        /*
         * 是云顶景区的话每总套票循环时多三层tr
         * 第一层景点
         * 第二层观光车
         * 第三层索道
         * 第四层酒店
         */
        ?>
        <tr data-id="jd">
            <td><?=++$m?></td>
            <td><?=$val['jname']?></td>
            <td><?=$dname[$val['dname']]?></td>
            <td><?=$val['tnum']?></td>
            <td><?=$val['jti_num']?></td>
            <td><?=$val['jmoney']/100?></td>
            <td><?=$val['jmoney']/100?></td>
            <td><?=0?></td>
            <td><?=0?></td>
            <td><?=0?></td>

            <td class="tL"></td>
            <td><?=pmode($key)?></td>
        </tr>
        <tr data-id="ggc">
            <td><?=++$m?></td>
            <td><?=$val['cname']?></td>
            <td><?=$dname[$val['dname']]?></td>
            <td><?=$val['tnum']?></td>
            <td><?=$val['cti_num']?></td>
            <td><?=$val['cmoney']/100?></td>
            <td><?=0?></td>
            <td><?=$val['cmoney']/100?></td>
            <td><?=0?></td>
            <td><?=0?></td>
            <td class="tL"></td>
            <td><?=pmode($key)?></td>
        </tr>
        <tr data-id="sd">
            <td><?=++$m?></td>
            <td><?=$val['sname']?></td>
            <td><?=$dname[$val['dname']]?></td>
            <td><?=$val['tnum']?></td>
            <td><?=$val['sti_num']?></td>
            <td><?=$val['smoney']/100?></td>
            <td><?=0?></td>
            <td><?=0?></td>
            <td><?=$val['smoney']/100?></td>
            <td><?=0?></td>
            <td class="tL"></td>
            <td><?=pmode($key)?></td>
        </tr>
        <?if($ticket_data[$k]['data'][3]){?>
        <tr data-id="hotel">
            <td><?=++$m?></td>
            <td><?=$val['hname']?></td>
            <td><?=$dname[$val['dname']]?></td>
            <td><?=$val['tnum']?></td>
            <td><?=$val['hti_num']?></td>
            <td><?=$val['hmoney']/100?></td>
            <td><?=0?></td>
            <td><?=0?></td>
            <td><?=0?></td>
            <td><?=$val['hmoney']/100?></td>
            <td class="tL"></td>
            <td><?=pmode($key)?></td>
        </tr>
        <?}}?>
        <?}}}//三个foreach的循环后半部分的括号?>
        </tbody>
        <tfoot>
        <?php else:?>
            <tr>
                <td align="left" colspan="9">&nbsp;&nbsp;&nbsp;交易日期无销售订单！</td>
            </tr>
        <?php endif;?>
        </tfoot>
    </table>
</div>
</div>
<script>
    $("#txtStartTime").bind("click focus", function () {

        var endtimeTf = $dp.$('txtEndTime');
        WdatePicker({
            maxDate: '#F{$dp.$D(\'txtEndTime\')}',
            dateFmt: "yyyy-MM-dd",
            onpicked: function () { endtimeTf.focus(); }
        });
    });
    $("#txtEndTime").bind("click focus", function () {
        WdatePicker({
            minDate: '#F{$dp.$D(\'txtStartTime\')}',
            dateFmt: "yyyy-MM-dd"
        });
    });
</script>

